package com.probox.common.core.utils;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 高德api
 */
public class GaoDeUtil {

    private static final String key = "6acd54781f506116f1ab5761032f2a13";

    /**
     * 根据地址获取经纬度
     *
     * @param path
     * @return
     */
    public static List<String> geo(String path) {
        if (ObjectUtil.isNotEmpty(path)) {
            String purl = "https://restapi.amap.com/v3/geocode/geo?key=" + key + "&address=" + path;
            String s = HttpUtil.get(purl);
            JSONObject jsonObject = JSONObject.parseObject(s);
            if ("OK".equals(jsonObject.getString("info"))) {
                JSONArray geocodes = jsonObject.getJSONArray("geocodes");
                return Arrays.asList(geocodes.getJSONObject(0).getString("location").split(","));
            }
        }
        return new ArrayList<>();
    }
}
